package com.google.code.customercare.interceptor;

import java.util.Map;

import org.apache.commons.configuration.PropertiesConfiguration;

import com.google.code.customercare.common.Constants;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;


public class AuthenticationAuthorizationInterceptor implements Interceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String intercept(ActionInvocation actionInvocation) throws Exception {
		PropertiesConfiguration config = new PropertiesConfiguration("customercare.properties");
		if("0".equals(config.getString("int_auth_authz"))) {
			return actionInvocation.invoke();
		}
		Map<String, Object> session = actionInvocation.getInvocationContext().getSession();
		
		String userName = (String) session.get(Constants.USER_KEY);
		// Integer id = (Integer) session.get(Constants.ID_KEY);
		boolean isAuthenticated = (userName!=null);
		if(!isAuthenticated){
			return Action.LOGIN;
		}
		else{
			return actionInvocation.invoke();
		}
	}

}
